package Parser;

import java.util.ArrayList;

import model.CharacterParameters;
import model.Dimension;
import model.ObjectRepresentation;
import model.Position;
import model.Sprite;
import model.SpriteSequence;

import org.jdom2.Element;

public class GenerateEnemyParameters {

	public GenerateEnemyParameters(ParserJDOM parserJDOM,
			CharacterParameters enemyParameters) {

		parserJDOM.resetRoot();
		Element element = parserJDOM.searchElement("enemyParametres");
		Element element2 = parserJDOM.searchElement("initialRepresentation");

		ObjectRepresentation objectRepresentation = new Sprite();
		objectRepresentation.setId(element2.getChild("id").getValue());
		enemyParameters.setInitialRepresentation(objectRepresentation);

		parserJDOM.setCurrent(element);
		element2 = parserJDOM.searchElement("initialPosition");
		Position position = new Position();
		position.setColumn(Integer.valueOf(element2.getChild("column")
				.getValue()));
		position.setRow(Integer.valueOf(element2.getChild("row").getValue()));
		enemyParameters.setInitialPosition(position);

		parserJDOM.setCurrent(element);
		element2 = parserJDOM.searchElement("movementBordersDimension");
		Dimension dimension = new Dimension();
		dimension.setColumn(Integer.valueOf(element2.getChild("column")
				.getValue()));
		dimension.setRow(Integer.valueOf(element2.getChild("row").getValue()));
		dimension.setHeight(Integer.valueOf(element2.getChild("height")
				.getValue()));
		dimension.setWidth(Integer.valueOf(element2.getChild("width")
				.getValue()));
		enemyParameters.setMovementBordersDimension(dimension);

		parserJDOM.setCurrent(element);
		element2 = parserJDOM.searchElement("movementSpriteSequence");
		SpriteSequence spriteSequence = new SpriteSequence();
		ArrayList<Sprite> listSprite = new ArrayList<Sprite>();
		for (Element child : element2.getChildren()) {
			if (child.getName().equals("id")) {
				spriteSequence.setId(child.getValue());
			} else {
				System.out.println();
				Sprite sprite = new Sprite();
				sprite.setId(child.getChild("id").getValue());
				if (child.getChild("conditionalColumn") != null) {
					sprite.setConditionalColumn(Integer.valueOf(child.getChild(
							"conditionalColumn").getValue()));
				}
				if (child.getChild("conditionalRow") != null) {
					sprite.setConditionalRow(Integer.valueOf(child.getChild(
							"conditionalRow").getValue()));
				}
				sprite.setPath(child.getChild("path").getValue());
				listSprite.add(sprite);
			}
		}
		spriteSequence.setSprites(listSprite);
		enemyParameters.setMovementSpriteSequence(spriteSequence);


	}

}
